package xiegd.study;

import lombok.Builder;
import lombok.Data;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

/**
 * @Author: xieguangda
 * @Description:
 * @Date: 2023/9/6 15:41
 */
public class tet003 {

    public static void main(String[] args) {
        Student student = Student.builder().name("我是测试").age(10).build();
        //Teacher t = Teacher.builder().name2("我是测试").age2(10).build();
        Student student1 = Student.builder().name("我是测试2").age(110).build();
        List<String> strings = compareObjects(student, student1);
        System.out.println("我是列表展示：" + strings);
    }

    public static List<String> compareObjects(Object obj1, Object obj2) {
        List<String> list = new ArrayList<>();

        Class<?> clazz = obj1.getClass();
        Field[] fields = clazz.getDeclaredFields();

        for (Field field : fields) {
            String fieldName = field.getName();
            field.setAccessible(true);
            try {
                Object value1 = field.get(obj1);
                Object value2 = field.get(obj2);

                if ((value1 == null && value2 != null) || (value1 != null && !value1.equals(value2))) {
                    list.add(fieldName);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return list;
    }


    public <T> List<T> readDataFromFile(String filePath, Function<String, T> converter) throws IOException {
        List<T> result = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                result.add(converter.apply(line));
            }
        }
        return result;
    }

    //余额读取调用
   // List<BalanceDTO> resultListOfA = readDataFromFile(balancePathOfA, BalanceDTO::convert);
    //明细读取调用
   // List<DetailDTO> resultList = readDataFromFile(detailPath, DetailDTO::convert);
}
@Data
@Builder
class Student{
    private String name;

    private Integer age;
}
@Data
@Builder
class Teacher{

    private String name;

    private Integer age;
}
